<?php

class Webcms extends Admin_Controller {

    function Webcms() {
        parent::Admin_Controller();

        $this->_data['erreur'] = "";
    }

    function index() {
        $this->list_admin();
    }

    function list_admin() {
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/webcms/list_admin/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('admin');
        $this->_data['query'] = $this->admin_m->get_admin($config['per_page'], $offset);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->db->count_all('admin');
        $this->_data['page_title'] = "Liste de Admin";
        $this->display_view('admin/admin');
    }

    function add_admin() {
        is_admin();
        $this->_data['erreur'] = "";
        $config = array(
            array(
                'field' => 'nom',
                'label' => 'Nom',
                'rules' => 'required'
            ),
            array(
                'field' => 'ident',
                'label' => 'Identifiant',
                'rules' => 'required'
            ),
            array(
                'field' => 'pass',
                'label' => 'Mot de passe',
                'rules' => 'required|matches[confirm_pass]|md5'
            ),
            array(
                'field' => 'confirm_pass',
                'label' => 'Confirmation du mot de passe',
                'rules' => 'required|md5'
            )
        );
        $this->form_validation->set_rules($config);
        $this->_data['erreur'] = "";
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'nom' => $this->input->post('nom'),
                    'ident' => $this->input->post('ident'),
                    'pass' => $this->input->post('pass'),
                    'droit' => '1'
                );
                $name = $data['ident'];
                $query = $this->admin_m->is_name_existe($name);
                if (!$query) {

                    $query = $this->admin_m->add_admin($data);
                    if ($query)
                        redirect(site_url('webcms/list_admin'));
                    else {
                        $this->_data['erreur'] = "Erreur lors de l'inscription !";
                        $this->_data['page_title'] = "Add admin";
                        $this->display_view('admin/add_admin');
                    }
                } else {
                    $this->_data['erreur'] = "Email existe !";
                    $this->_data['page_title'] = "Add admin";
                    $this->display_view('admin/add_admin');
                }
            } else {
                $this->_data['page_title'] = "Add admin";
                $this->display_view('admin/add_admin');
            }
        } else {
            $this->_data['page_title'] = "Add admin";
            $this->display_view('admin/add_admin');
        }
    }

    function login($log='') {

        if ($log == "check") {
            $this->form_validation->set_rules('ident', 'Identifiant', 'required');
            $this->form_validation->set_rules('pass', 'Mot de passe', 'required');
            $this->form_validation->set_message('required', '%s est oblig&eacute;');
            $username = $this->input->post('ident');
            $password = md5($this->input->post('pass'));
            if ($this->form_validation->run() == FALSE) {

                $this->load->view('admin/login', $this->_data);
            } else {
                $query = $this->admin_m->login($username, $password);
                if ($query->num_rows() > 0) {
                    $row = $query->row();
                    $this->session->set_userdata('userid', $row->id);
                    $this->session->set_userdata('username', $row->ident);
                    $this->session->set_userdata('userlevel', $row->droit);
                    redirect(base_url() . 'index.php/webcms');
                } else {
                    $this->_data['erreur'] = "Identifiant ou mot de passe n'existe pas";
                    $this->load->view('admin/login', $this->_data);
                }
            }
        }else
            $this->load->view('admin/login', $this->_data);
    }

    function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . 'index.php/webcms');
    }

    function delete_admin($id) {
        is_admin();
        if ($this->session->userdata('userlevel') == 2) {
            $query = $this->admin_m->delete_admin($id);
        }
        redirect(site_url('webcms'));
    }

    function compte($action='') {
        is_admin();
        $idadmin = $this->session->userdata('userid');
        $admin = $this->admin_m->get_detail_admin($idadmin);
        $this->_data['admin'] = $admin;
        if ($action == "profil") {
            $this->_data['erreur'] = "";
            $config = array(
                array(
                    'field' => 'ident',
                    'label' => 'Identifiant',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run()) {
                $data = array('ident' => $this->input->post('ident'));
                $name = $data['ident'];
                $query = $this->admin_m->is_name_existe($name);
                if (!$query) {
                    $query = $this->admin_m->update_admin($data, $idadmin);
                    redirect(site_url('webcms/compte'));
                } else {
                    $this->_data['erreur'] = "Identifiant existe";
                    $this->display_view('admin/compte');
                }
            }else
                $this->display_view('admin/compte');
        }
        elseif ($action == "pass") {
            $config = array(
                array(
                    'field' => 'apass',
                    'label' => 'Ancien mot de passe',
                    'rules' => 'required|md5'
                ),
                array(
                    'field' => 'npass',
                    'label' => 'Nouveau mot de passe',
                    'rules' => 'required|matches[confirm_npass]|md5'
                ),
                array(
                    'field' => 'confirm_npass',
                    'label' => 'Confirmation du mot de passe',
                    'rules' => 'required|md5'
                )
            );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run()) {
                $pass = $this->input->post('apass');
                $query = $this->admin_m->is_password($pass, $idadmin);
                if ($query) {
                    $data = array('pass' => $this->input->post('npass'));
                    $query = $this->admin_m->update_admin($data, $idadmin);
                    redirect(site_url('webcms/compte'));
                } else {
                    $this->_data['erreur'] = "Mot de passe incorrect";
                    $this->display_view('admin/compte');
                }
            }else
                $this->display_view('admin/compte');
        } else
            $this->display_view('admin/compte');
    }

}

?>